package itsm.isperp.module.controller.incident;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.incident.IcdCategory;
import itsm.isperp.module.service.incident.IcdCategoryService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 事件 分类管理控制器
 * 
 * @author lizx
 * @date 2013-9-6
 */
@Controller
@RequestMapping("incident/category")
public class IcdCategoryController extends TreeNodeController<IcdCategory> {

	@Autowired
	IcdCategoryService icdCategoryService;

	@Override
	public IcdCategoryService getService() {
		return icdCategoryService;
	}

	@Override
	public String getPageTitle() {
		return "事件分类";
	}

	@RequestMapping("relateEgEdit")
	public ModelAndView roleUserEdit(String roleId) {
		ModelAndView mv = new ModelAndView("incident/category/relateEgEdit");
		mv.addObject("roleId", roleId);
		System.out.print("ininin`==================");

		/*
		 * AppRole role = this.getService().findOne(roleId);
		 * mv.addObject("roleName", role.getName());
		 */
		return mv;

	}

	 
	
	/**
	 * 获取事件分类关联人员列表
	 * 
	 * @param categoryId
	 *            事件分类Id
	 * @return 关联工程师
	 */
	@RequestMapping("categoryRelateMemberUserList")
	@ResponseBody
	public List<Map<String, Object>> categoryRelateMemberUserList(String categoryId) {

		categoryId=categoryId.replace(",",new String());
		List<Map<String, Object>> resultList=this.icdCategoryService.getIcdCategoryRelamembers(categoryId);
		return resultList;

	}
	
	@RequestMapping("list")
	@ResponseBody
	public PageResponse<IcdCategory> list(JqGridRequest request,
			HttpServletRequest req) {
		String term = req.getParameter("term");
		if (StringUtils.isNotEmpty(term)) {
			request.isPageable(false);
			request.setRows(15);
			request.setSearchString(" name LIKE '%" + term
					+ "%'");
			return this.getService().findAll(request).toPage();

		} else {
			return this.getService().findAll(request).toPage();
		}

	}
	
	@RequestMapping("getFirstStepUsers")
	@ResponseBody
	public List<Map<String,Object>> getFirstStepUsers(JqGridRequest request,String categoryId,
			HttpServletRequest req) {
		String sql="select description from app_dict where type_code='ICD_FIRSTSTEP_SUPPORTERS' AND code='"+categoryId+"'";
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		String filter=jdbcTemplate.queryForObject(sql, String.class);
		String sql1 = "select name,fullname from (SELECT b.name,b.fullname,a.name role_name FROM v_app_user_role a INNER JOIN app_user b ON a.app_user_name=b.name) t where 1=1 "+filter;
		return jdbcTemplate.queryForList(sql1);
	}
}
